package hw20170814.q1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Dbutils {
	private Connection conn=null;
	private PreparedStatement pst=null;
	private ResultSet rs=null;
	
	String url="jdbc:mysql://127.0.0.1:3306/examsystem?useUnicode=true&characterEncoding=GBK";
	String dbname="root";
	String dbpwd="root123";
	public Connection getConnection() throws ClassNotFoundException, SQLException{
		Class.forName("com.mysql.jdbc.Driver");
		return DriverManager.getConnection(url, dbname, dbpwd);
	}
	
	public void closeAll(){
		if (rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pst!=null) {
			try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

}
